package org.jeecg.modules.system.controller.website;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysTitle;
import org.jeecg.modules.system.entity.website.SysTitleTreeModel;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.service.ISysTitleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 标题
 * @Author: jeecg-boot
 * @Date:   2024-06-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags="标题")
@RestController
@RequestMapping("/sys/website/sysTitle")
public class SysTitleController extends JeecgController<SysTitle, ISysTitleService> {


	@Autowired
	private ISysTitleService sysTitleService;


	/**
	 * 分页列表查询
	 *
	 * @param sysTitle
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "标题-分页列表查询")
	@ApiOperation(value="标题-分页列表查询", notes="标题-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysTitle sysTitle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysTitle> queryWrapper = QueryGenerator.initQueryWrapper(sysTitle, req.getParameterMap());
		Page<SysTitle> page = new Page<SysTitle>(pageNo, pageSize);
		IPage<SysTitle> pageList = sysTitleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param sysTitle
	 * @return
	 */
	@AutoLog(value = "标题-添加")
	@ApiOperation(value="标题-添加", notes="标题-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysTitle sysTitle) {
		sysTitleService.save(sysTitle);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param sysTitle
	 * @return
	 */
	@AutoLog(value = "标题-编辑")
	@ApiOperation(value="标题-编辑", notes="标题-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody SysTitle sysTitle) {
		sysTitleService.updateById(sysTitle);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "标题-通过id删除")
	@ApiOperation(value="标题-通过id删除", notes="标题-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysTitleService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "标题-批量删除")
	@ApiOperation(value="标题-批量删除", notes="标题-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysTitleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "标题-通过id查询")
	@ApiOperation(value="标题-通过id查询", notes="标题-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysTitle sysTitle = sysTitleService.getById(id);
		return Result.OK(sysTitle);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param sysTitle
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, SysTitle sysTitle) {
      return super.exportXls(request, sysTitle, SysTitle.class, "标题");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SysTitle.class);
  }





	 @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	 public Result<List<?>> queryTreeList(@RequestParam(name = "ids", required = false) String ids) {
		 Result<List<?>> result = new Result<>();
		 result.setCode(CommonConstant.SC_OK_200);
		 try {
//          从内存中读取
//			List<SysDepartTreeModel> list =FindsDepartsChildrenUtil.getSysDepartTreeList();
//			if (CollectionUtils.isEmpty(list)) {
//				list = sysDepartService.queryTreeList();
//			}
			 if(oConvertUtils.isNotEmpty(ids)){
				 List<SysTitleTreeModel> departList = sysTitleService.queryTreeList(ids);
				 result.setResult(departList);
			 }else{
				 List<SysTitleTreeModel> list = sysTitleService.queryTreeList();
				 result.setResult(list);
			 }
			 result.setSuccess(true);
		 } catch (Exception e) {
			 log.error(e.getMessage(),e);
		 }
		 return result;
	 }



	 @RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
	 public Result<List<DepartIdModel>> queryIdTree() {
//		Result<List<DepartIdModel>> result = new Result<List<DepartIdModel>>();
//		List<DepartIdModel> idList;
//		try {
//			idList = FindsDepartsChildrenUtil.wrapDepartIdModel();
//			if (idList != null && idList.size() > 0) {
//				result.setResult(idList);
//				result.setSuccess(true);
//			} else {
//				sysDepartService.queryTreeList();
//				idList = FindsDepartsChildrenUtil.wrapDepartIdModel();
//				result.setResult(idList);
//				result.setSuccess(true);
//			}
//			return result;
//		} catch (Exception e) {
//			log.error(e.getMessage(),e);
//			result.setSuccess(false);
//			return result;
//		}
		 Result<List<DepartIdModel>> result = new Result<>();
		 try {
			 List<DepartIdModel> list = sysTitleService.queryTitleIdTreeList();
			 result.setCode(CommonConstant.SC_OK_200);
			 result.setResult(list);
			 result.setSuccess(true);
		 } catch (Exception e) {
			 log.error(e.getMessage(),e);
		 }
		 return result;
	 }




 }
